package auction;

import java.util.Date;

public class Auction {
    private int id;
    private String name;
    private String user;
    private Date expires;
    //private double bid;
    //private String biduser;
    private Bid highestBid;
    
    /**
     * Creates a new auction
     * @param name -Name of the auction
     * @param user -User who create the auction
     * @param time -seconds to last from Now
     */
    public Auction(Integer id, String name, String user, int time) {
        this.id = id;
        this.name = name;
        this.user = user;
        Date d = new Date();
        d.setTime(d.getTime()+time*1000);
        this.expires = d;
        this.highestBid = null;
       // this.bid = 0.0;
       // this.biduser = "";
    }   

    public Date getExpires() {
        return expires;
    }

    /**
     * Checks bid on Auction, if higher accepts bid, returns Feedback message to be sent to client.
     * @param user
     * @param amount
     * @return
     */
    public String bid(Bid bid){

            if (bid.isFromThisUser(user)){
                return "You can't bid on your own Auction!\n";
                
            } else if(highestBid == null) {
                highestBid = bid;
                AccountManager.instance.reportBidEvent("BID_PLACED", user, id, bid.getAmount());
                return "You successfully bid with "+bid.getAmount()+ " on '"+this.name+"'.\n";
            } else if (bid.isFromSameUser(highestBid)){
                return "You can't overbid yourself!\n";
            } else {
                if (bid.isHigher(highestBid)){
                    highestBid.overbidden(AccountManager.instance, bid, this.name, this.id);
                    highestBid = bid;
                    return "You successfully bid with "+bid.getAmount()+ " on '"+this.name+"'.\n";
                } else {
                    return "You unsuccessfully bid with "+bid.getAmount()+ " on '"+this.name+"'. Current highest bid is "+this.highestBid.getAmount()+".\n";
                }
            }

    }
    
   /* public String bid(String user, double amount){
        if (user.equals(this.user)){
            return "You can't bid on your own Auction!\n";      
        } else if (user.equals(this.biduser)){
            return "You can't overbid yourself!\n";
        } else {
            if (amount > bid){
                
                if (!this.biduser.equals("")){
                    System.out.println("Notify '"+this.biduser+"' that he has been overbidden");
                    AccountManager.instance.notifyUser(this.biduser, "!new-bid "+this.name+"\n");
                    AccountManager.instance.reportBidEvent("BID_OVERBID", user, id, amount);
                } else {
                    AccountManager.instance.reportBidEvent("BID_PLACED", user, id, amount);
                }
                
                this.bid = amount;
                this.biduser = user;
                return "You successfully bid with "+amount+ " on '"+this.name+"'.\n";
            } else {
                return "You unsuccessfully bid with "+amount+ " on '"+this.name+"'. Current highest bid is "+this.bid+".\n";
            }
        }
    }
    */
    
    @Override
    public String toString() {
        //Format: 1. 'Apple I' wozniak 10.10.2012 21:00 CET 10000.00 gates
        StringBuilder sb = new StringBuilder();
        sb.append(this.id).append(". ");
        sb.append("'").append(this.name).append("' ");
        sb.append(this.user).append(" ");
        sb.append(this.expires).append(" ");
        if (highestBid != null) {
            sb.append(this.highestBid.getAmount()).append(" ");
            sb.append(this.highestBid.getUser());
        } else {
            sb.append("0.0 none");
        }
        
        return sb.toString();
    }
    
    /**
     * Returns Auction end String
     * @return
     */
    public String getAuctionEndString() {
        ///!auction-ended dave 250.00 Super small notebook
        StringBuilder sb = new StringBuilder();
        sb.append("!auction-ended ");
        if (highestBid != null) {
            sb.append(this.highestBid.getAmount()).append(" ");
            sb.append(this.highestBid.getUser());
        } else {
            sb.append("0.0 none");
        }
        sb.append(" ");
        sb.append(this.name).append("\n");
        
        return sb.toString();
    }

    public String getUser() {
        return user;
    }

    public String getBiduser() {
        if (highestBid != null) {
            return highestBid.getUser();
        } else {
            return "none";
        }
    }

    public String getName() {
        return name;
    }

	/**
	 * @return the bid
	 */
	public double getBid() {
	    if (highestBid != null) {
	        return highestBid.getAmount();
	    } else {
	        return 0.0;
	    }
	}

    public Bid getHighestBid() {
        return highestBid;
    }

    /**
     * Checks bid on auction, if higher and in time accepts bid.
     * @param bid The bid.
     * @return Feedback message for the client.
     */
    public String delayedBid(Bid bid){
        if (bid.isFromThisUser(user)){
            return "You can't bid on your own Auction!";
        } else {
        	if (new Date(bid.getTimestamp()).before(expires)) {
        		if (highestBid == null || bid.isHigher(highestBid) ||
            		(bid.getAmount() == highestBid.getAmount() && bid.getTimestamp() < highestBid.getTimestamp())) {
        			highestBid = bid;
                    return "You successfully bid with "+bid.getAmount()+ " on '"+this.name+"'.";
        		} else {
                    return "You unsuccessfully bid with "+bid.getAmount()+ " on '"+this.name+"'. Current highest bid is "+this.highestBid.getAmount()+".";
                }
        	} else {
        		return "Your bid for " + name + " with " + bid.getAmount() + " was placed after expiration";
            }
        }
    }
    
	
}
